<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <title>上传数据--Instreet</title>
    <%include ../head%>
    <link rel="stylesheet" type="text/css" href="/css/datepicker.css">  
  </head>
  <body>
      <%include ../header%>
      <div id="container">
        <div class="container">
          <input type="hidden" id="field" value="<%=field%>"> 
          <input type="hidden" id="title"  value="<%=title%>"/>
          <h4 class="edit-form-top">
            <span>添加数据报表(<%=title%>)</span>
          </h4>
   
          <table class="table table-bordered table-striped">
            <thead>
              <th>日期</th>
              <th>请求次数</th>
              <th>曝光次数</th>
              <th>点击次数</th>
              <th>收入(人民币)</th>
            </thead>
            <tbody>
              <tr>
                <td>
                  <input id="datepicker" name="datepicker" class="input-text span2" maxlength="10" type="text"> 
                </td>
                <td>
                  <input class="input-text span2" type="text" id="adRequest" name="adRequest"/>
                </td>
                <td>
                  <input class="input-text span2" type="text" id="adView" name="adView"/>
                </td>
                <td>
                  <input class="input-text span2" type="text" id="adClick" name="adClick"/>
                </td>
                <td>
                  <span style="vertical-align: text-bottom;display: inline-block; font-size: 14px; font-family: arial; font-weight: bold; color: #FF4700;">￥</span>
                  <input class="input-text span2" type="text" id="adRevenue" name="adRevenue"/>
                </td>
                <td>
                  <div style="margin-bottom:10px;">
                    <a class="btn btn-primary btn-submit">添加数据</a>
                  </div>                                    
                  <div style="margin-bottom:10px;">
                    <a class="btn btn-primary btn-submit-auto">自动生成</a>
                  </div>  
                  <div style="position:relative;overflow:hidden;">
                    <input type="file" id="reportfile" name="reportfile" style="position: absolute;z-index: 99; top: 0; right: 0; opacity: 0;"/> 
                    <a class="btn btn-primary btn-success">导入Excel</a>
                  </div>                                    
                </td>
              </tr>
              <%reportData.forEach(function(data,i){
                if(data!=null){  
                  data = JSON.parse(data);
                %>
                <tr><td><%=data.date%></td><td><%=data.request%></td><td><%=data.view%></td><td><%=data.click%></td><td><%=data.revenue%></td><td></td></tr>
              <%}});%>
            </tbody>
          </table>

          <form class="form-horizontal">
            <div class="control-group">
              <label class="control-label" for="cnzzid">CNZZ监控ID</label>
              <div class="controls">
                <input type="text" name="cnzzid" id="cnzzid" placeholder="输入CNZZ监控ID" value="<%=setting.cnzzid||''%>">
              </div>
            </div>           
            <div class="control-group">
              <label class="control-label" for="maxpv">每日pv</label>
              <div class="controls">
                <input type="text" name="maxpv" id="maxpv" placeholder="每日pv" value="<%=setting.maxpv||''%>">
              </div>
            </div>
            <div class="control-group">
              <label class="control-label" for="cpm">CPM</label>
              <div class="controls">
                <input type="text" name="cpm" id="cpm" placeholder="输入CPM" value="<%=setting.cpm||''%>"><span>元</span>
              </div>
            </div>
            <div class="control-group">
              <label class="control-label" for="minAvailPer">最低投放比例</label>
              <div class="controls">
                <input type="number" name="minAvailPer" id="minAvailPer" placeholder="输入0-1间的数字" value="<%=setting.minAvailPer||''%>">
                <span class="text-warning">
                  *设置最低投放比例，如果（每日投放数据÷每日pv）低于该比例则不生成数据
                </span>                
              </div>
            </div>
            <div class="control-group">
              <label class="control-label">数据生成区间</label>
              <div class="controls">
                <input type="number" name="minPercent" id="minPercent" placeholder="输入0-1间的数字" value="<%=setting.minPercent||''%>">
                -
                <input type="number" name="maxPercent" id="maxPercent" placeholder="输入0-1间的数字" value="<%=setting.maxPercent||''%>">
                <span class="text-warning">
                  *设置自动生成数据的区间范围，例如0.9-0.92
                </span>
              </div>
            </div>  
            <div class="control-group">
              <label class="control-label"></label>
              <div class="controls">
                <a href="javascript:;" class="btn btn-info btn-saveinfo">
                  保存修改
                </a>
              </div>
            </div>                      
          </form>  

        </div>
      </div>
      <script type="text/javascript" src="<%=config.jsUrl%>sea-modules/seajs/seajs/2.1.1/sea.js"></script>
      <script type="text/javascript" src="<%=config.jsUrl%>sea-modules/config.js"></script>
      <script type="text/javascript">
        seajs.use('/js/main',function  (a) {
          a.activeNav();
          a.bindEvents();
        });
      </script>
      <script type="text/javascript" >        
        seajs.use(['/js/validate','/js/main','datepicker','ajaxfileupload'],function  (validate,main) {
          var d = new Date();
          var field = $('#field').val()
              ,title = $('#title').val()
              ;
          var dateOpt = { 
            language : 'zh-CN',     
            format : 'yyyy-mm-dd',
            weekStart : 1,
            todayHighlight:true,            
            endDate : [d.getFullYear(),d.getMonth()+1,d.getDate()].join('-')
          }
          $('#datepicker').datepicker(dateOpt).on('changeDate',function(){
            $('#datepicker').datepicker('hide');
          });  
          // 提交报表
          $('.btn-submit').on('click',function(){
            var $this = $(this);
            if($this.hasClass('disabled')){
              return false;
            }
            var obj = {
              field:field,
              date:$('#datepicker').val(),
              title:title,
              request:$('#adRequest').val(),
              view:$('#adView').val(),
              click:$('#adClick').val(),
              revenue:$('#adRevenue').val()
            };
            for(var i in obj){
              if(!$.trim(obj[i])){
                alert('数据不全哦');
                return;
              }
            };
            $this.addClass('disabled');
            $.post('/widget/addReport',obj,function(data){
                if(data){
                  $this.removeClass('disabled');     
                  if(data.err){
                    alert('添加数据出错，重新来一次试试')
                  }else{
                    location.reload();
                  }
                }
              })
          });
          
          // 生成数据
          $('.btn-submit-auto').on('click',function(){
            $.get('/widget/generateReportData',{field:field},function(data){
              if(!data||data.err){
                alert('生成数据失败');
              }else{
                location.reload();
              }
            })
          });

          // 保存设置
          $('.btn-saveinfo').on('click',function(){
            var data = {
              field:field,
              title:title,
              cnzzid: $.trim($('#cnzzid').val()),
              maxpv:$('#maxpv').val(),
              cpm:$('#cpm').val(),
              minAvailPer:$('#minAvailPer').val(),
              minPercent:$('#minPercent').val(),
              maxPercent:$('#maxPercent').val()
            };
            for(var i in data){
              if(!data[i]){
                alert('数据不能为空');
                return;
              }
            }
            $.post('/widget/setReportSetting',data,function(res){
              if(res){
                if(res.err){
                  alert(res.err)
                }else{
                  alert('保存成功')
                }                
              }
            });
          });

          //异步上传excel文件
          $('#reportfile').AjaxFileUpload ({
            action:"/widget/importReport",
            onComplete:function(name,result){
              if(result){                             
                $.post('/widget/addMReport',{field,title:title,dataList:result},function(result){
                    if(!result.err){
                      location.reload();
                    }else{
                      alert(result.err);
                    }
                });
              }
            }
          });

        });
      </script>
      <%include ../footer%>
 </body>
 </html>